<?php
/**
 * Created by PhpStorm.
 * User: 郑合达
 * Date: 2018/12/20
 * Time: 15:31
 */

namespace app\admin\controller;
use app\Model\CouponModel;
use app\Model\UsersModel;
use app\Model\CouponListModel;
class Coupon extends Comment
{
    private $page = 10;
    /*
    * 优惠券类型列表
    */
 public function index(){
     $this->getConAndAct('counp');
     $lists = CouponModel::order('add_time desc')->where('type','=',1)->paginate($this->page);
     $this->assign('lists',$lists);
     $coupons =["面额模板","按用户发放","注册发放","邀请发放","线下发放"];
     $this->assign('coupons',$coupons);
     return view('index');
 }
    /*
       * 添加一个优惠券类型
       */
    public function coupon_info(){
        if(request()->isPost()){
            $data['name']=input('name');
            $data['money']=input('money');
            $data['condition']=input('condition');
            $data['type']=input('type');
            $data['createnum']=input('createnum');

            $data['send_start_time'] = strtotime(input('send_start_time'));
            $data['send_end_time'] = strtotime(input('send_end_time'));
            $data['use_end_time'] = strtotime(input('use_end_time'));
            $data['use_start_time'] = strtotime(input('use_start_time'));
            if(input('send_start_time') > input('send_end_time')){
                return retJson('',201,'发放日期填写有误');
            }
                $data['add_time'] = time();

                $row = CouponModel::create($data);
            if(!$row){
                return retJson('',202,'添加失败');
            }
            return retJson('',200,'添加成功');
        }
        $cid = input('id');
        if($cid){
            $coupon =CouponModel::where('id','=',$cid)->find();
            $this->assign('coupon',$coupon);
        }else{
            $def['send_start_time'] = strtotime("+1 day");
            $def['send_end_time'] = strtotime("+1 month");
            $def['use_start_time'] = strtotime("+1 day");
            $def['use_end_time'] = strtotime("+2 month");
            $this->assign('coupon',$def);
        }
       return view('add_coupon');
    }
    /*
       * 编辑优惠券类型
       */
    public function coupon_edit(){
        if(request()->isPost()){
            $data['name']=input('name');
            $data['money']=input('money');
            $data['condition']=input('condition');
            $data['type']=input('type');
            $data['createnum']=input('createnum');

            $data['send_start_time'] = strtotime(input('send_start_time'));
            $data['send_end_time'] = strtotime(input('send_end_time'));
            $data['use_end_time'] = strtotime(input('use_end_time'));
            $data['use_start_time'] = strtotime(input('use_start_time'));
            if(input('send_start_time') > input('send_end_time')){
                return retJson('',201,'发放日期填写有误');
            }
            $data['add_time'] = time();
            $cid = input('id');
            $row = CouponModel::update($data, ['id' => $cid]);
            if(!$row){
                return retJson('',202,'修改失败');
            }
            return retJson('',200,'修改成功');
        }
        $cid = input('id');
            $coupon =CouponModel::where('id','=',$cid)->find();
            $this->assign('coupon',$coupon);
        return view('edit_coupon');
    }

    /**
     * 按用户发放优惠券页面
     */
    public function send_coupon(){
        $cid = input('cid');
        $level = Db('tp_user_level')->select();//用户等级
       //搜索条件
        $condition = array();
        input('mobile') ? $condition['mobile'] = input('mobile') : false;
        input('email') ? $condition['email'] = input('email') : false;
        $nickname = input('nickname');
        if(!empty($nickname)){
            $condition['nickname'] = array('like',"%$nickname%");
        }
        $userList = UsersModel::where($condition)->order("user_id desc")->paginate($this->page,false,['query'=>request()->param()]);
        $user_level = Db('tp_user_level')->column('level_id,level_name');
        $this->assign('user_level',$user_level);
        $this->assign('userList',$userList);
        $this->assign('level',$level);
        $this->assign('cid',$cid);
      return view('send_coupon');
    }

    /**
     * 发放优惠券
     */
    public function fafang(){
             $cid = input('cid');
            $user_id = input('user_id');
            $level_id = input('level_id');
            $insert = [];
             $pieces = explode(",",rtrim($user_id, ','));
;            $coupon = CouponModel::where('id','=',$cid)->find();
            if($coupon['createnum']>0){
                $remain = $coupon['createnum'] - $coupon['send_num'];//剩余派发量
                if($remain<=0) {
                    return retJson('',202,$coupon['name'].'已经发放完了');
                }
            }
            //发放1
            if(empty($user_id) && $level_id>=0){
                if($level_id==0){
                    $user = UsersModel::where('is_lock','=',0)->select();
                }else{
                    $where['is_lock']=0;
                    $where['level']=$level_id;
                    $user = UsersModel::where($where)->select();
                }
                if($user){
                    $able = count($user);//本次发送量
                    if($coupon['createnum']>0 && $remain<$able){
                        return retJson('',204,$coupon['name'].'派发量只剩'.$remain.'张');
                    }
                    foreach ($user as $k=>$val){
                        $user_id = $val['user_id'];
                        $time = time();
                        $data = [
                            'cid'=>$cid,
                            'type'=>1,
                            'uid'=>$user_id,
                            'send_time'=>$time
                        ];
                        array_push($insert,$data);
                    }
                }
            }else {
                //发放2
                $able = count($pieces);//本次发送量
                if ($coupon['createnum'] > 0 && $remain < $able) {
                    return retJson('', 203, $coupon['name'] . '派发量只剩' . $remain . '张');
                }
                foreach ($pieces as $k => $v) {
                    $time = time();
                    $data = [
                        'cid' => $cid,
                        'type' => 1,
                        'uid' => $v,
                        'send_time' => $time
                    ];
                    array_push($insert, $data);
                }
            }
              CouponListModel::insertAll($insert);
             CouponModel::where('id','=',$cid)->setInc('send_num',$able);
             return retJson('',200,'发放'.$able.'张'.$coupon['name']);
    }


    /*
    * 删除优惠券类型
    */
    public function del_coupon(){
        //获取优惠券ID
        $cid = input('id');
        //查询是否存在优惠券
        $row =CouponModel::destroy($cid);
        if($row){
            //删除此类型下的优惠券
            Db('tp_coupon_list')->where(array('cid'=>$cid))->delete();
            return retJson('', 200, '删除成功！');
        }else{
            return retJson('', 201, '删除失败！');
        }
    }
}